/**
 * Created by lwy on 2017/5/22.
 */

var card=GetQueryString('card');
var flag= true;
var ifhascard;//0为未添加信息，1为有信息
var cardId='';
var accountName='';
var cardNum='';
var bank='';
var bankName='';
var userName='';
var flag2=0;
var kaihuhang='';
var page = 1;
var serviceCharge='';
$(function () {
    $('#money').val('');
    $.ajax({
        url:'/tiXianC/getTiXianFactorage.json',
        dataType:'json',
        data:{},
        type:'post',
        success:function (data) {
            serviceCharge = data.serviceCharge;
            $('#serviceCharge').html(serviceCharge*100);
            $('#serviceCharge2').html(serviceCharge*100);
        }
    })
})
$(function(){
    //银行卡跳入显示绑定银行卡盒子
    if(card=='false'){
        $('.mui-title').html('绑定银行卡');
        $('.bank-bound-box').show();
        $('.bank-bound-box').siblings().hide();
        $('#record').hide();
    }
})

$('#select').click(function(){
    //$('#kong').hide();
})

//计算手续费
var withMoney = document.getElementById('money');
withMoney.oninput=function(){
    if($('#money').val()!=''){
        $('#usable').hide();
        $('#ifover').hide();
        $('#service').show();
        var withMoney2 = $('#money').val();
        $('#poundage').html((withMoney2*serviceCharge).toFixed(2));
    }else{
        $('#poundage').html('0.00');
    }
    if(parseFloat($('#money').val())>parseFloat($('#accountMoney').html())){
        $('#usable').hide();
        $('#ifover').show();
        $('#service').hide();
    }
}

//显示银行卡信息
$(function(){
    $.ajax({
        url:'/cardC/getCardList.json',
        dataType:'json',
        type:'post',
        data:{
            token:getItem('token'),
            pageNu:page,
        },
        success:function(data){
            if(data.code==-1){
                window.location.href="login.html";
            }
            if(data.code==0){
                mui.toast(data.msg);
            }
            if(data.code==1){
                ifhascard=1;
                $('#accountMoney').html(data.datas.accountMoney);
                var data = data.datas.cardList;
                if(data.length==0){
                    if(card=='false'){

                    }else{
                        mui.confirm( '请先添加一张银行卡','您还没有可用于积分兑换的银行卡', ['取消', '确认'], function(e) {
                            if (e.index == 1) {
                                window.location.href="integral-withdraw1.html?card=false";
                                //$('.bank-bound-box').show();
                                //$('.tips').hide();
                                //$('.withdraw-box').hide();
                                //ifhascard=0;
                            } else {
                                window.location.href="personal.html";
                            }
                        })
                    }
                }
                for(var i=0;i<data.length;i++){
                    if(data[i].isDefault){
                        accountName=data[i].accountName;
                        cardNum=data[i].cardNum;
                        cardId=data[i].id;
                        bank=data[i].bankId;
                        bankName = data[i].bankName;
                        userName=data[i].userKey;
                        kaihuhang = data[i].kaihuhang;
                        $('#name').html(data[i].accountName);
                        $('#bank').html(data[i].bank);
                        $('#cardNumShort').html('尾号'+data[i].cardNumShort+'');
                        $('#bankImg').attr('src',data[i].bankImg);
                        $('#accountName').val(data[i].accountName);
                        $('#branchName').val(data[i].kaihuhang);
                        $('#bankNo').val(data[i].cardNum);
                        $('#kong').html(data[i].bank);
                        $('#kong').val(data[i].bankId);
                    }
                }
            }
        }
    })
})

//绑定银行卡
$('#bankConfirm').click(function(){
    if(flag!=true){
        mui.toast('正在请求中，请稍候...');
        return;
    }
    if($('#bankNo').val()!=$('#bankNo2').val()){
        mui.toast('两次输入的银行卡号不相同');
        flag=true;
        return;
    }
    flag=false;
    //var url='';
    //if(ifhascard==0){
    //    url='/cardC/addCard.json';
    //}else if(ifhascard==1){
    //    url='/cardC/updateCard.json';
    //}
    $.ajax({
        url:'/cardC/addCard.json',
        dataType:'json',
        type:'post',
        data:{
            token:getItem('token'),
            accountName:$('#accountName').val(),
            cardNum:$('#bankNo').val(),
            bankId:$('#select').val(),
            cardId:cardId,
            kaihuhang:$('#branchName').val(),
        },
        success:function(data){
            if(data.code==-1){
                window.location.href="login.html";
            }
            if(data.code==0){
                mui.toast(data.msg);
                flag=true;
            }
            if(data.code==1){
                mui.alert('绑定银行卡成功',function(){
                    window.location.href="personal.html";
                });
            }
        }
    })
})

//点击更改密码框属性
$('#password').focus(function(){
    $('#password').attr('type','password');
})

//申请提现
var certain = document.getElementById('certain');
certain.addEventListener('tap',function(){
    if($('#password').val()==''){
        mui.toast('交易密码不能为空');
        flag2=0;
        return;
    }
    var contents = $('#money').val();
    if(!isNaN(contents)){
        contents = 1 * contents;
        if(contents > 0 && contents % 100 == 0){

        }else{
            mui.toast("提现金额必须是整百");
            flag2=0;
            return;
        }
    }
    if(flag2!=0){
        mui.toast('正在请求，请稍候...');
        return;
    }
    flag2=1;
    var  passWord= calcMD5($('#password').val() + userName, 32);
        $.ajax({
            url:'/tiXianC/addTiXianShenQing.json',
            dataType:'json',
            type:'post',
            data:{
                token:getItem('token'),
                money:$('#money').val(),
                accountName:accountName,
                password:passWord,
                bankId:bank,
                cardId:cardId,
                cardNum:cardNum,
                factorage:$('#poundage').html(),
                kaihuhang:kaihuhang,
            },
            success:function(data){
                if(data.code==0){
                    mui.toast(data.msg);
                    flag2=0;
                    return;
                }
                if(data.code==-1){
                    flag2=0;
                    window.location.href="login.html";
                }
                if(data.code==1){
                    flag2=0;
                    window.location.href="withdraw-detail.html?id="+data.datas.id;
                }
            }
        })
})

$('.newWithdrawBankBox').click(function(){
    window.location.href="choosecard.html"
})